<template>
  <common-list>
    <!-- 头部搜素start -->
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        :model="query"
        ref="queryForm"
        @submit.native.prevent
        v-show="showSearch"
        :inline="true"
        label-width="105px"
      >
        <el-form-item label="时间服务器地址" prop="address">
          <common-input
            v-model="query.address"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="名称" prop="timeSynName">
          <common-input
            v-model="query.timeSynName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>
      </el-form>
    </common-search>
    <!-- 头部搜素end -->

    <!-- 按钮组织start -->
    <el-row slot="tools" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="edit(null)"
          v-hasPermi="['system:time:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="del(selectionIds)"
          v-hasPermi="['system:time:delete']"
          >批量删除
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮组织end -->
    <!--列表 start-->
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column
        label="名称"
        align="left"
        prop="timeSynName"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">{{ row.timeSynName }}</template>
      </el-table-column>

      <el-table-column
        label="编码"
        align="left"
        prop="timeSynCode"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">{{ row.timeSynCode }}</template>
      </el-table-column>

      <el-table-column label="时间服务器地址" align="left" prop="address">
        <template slot-scope="{ row }">{{ row.address }}</template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="left"
        prop="remark"
        show-overflow-tooltip
      >
        <template slot-scope="{ row }">{{ row.remark }}</template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="enable" width="80px">
        <template slot-scope="{ row }">
          <el-tag type="success" v-if="row.enable == 0">启用</el-tag>
          <el-tag type="danger" v-else>停用</el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column label="开始时间" align="left" prop="beginTime">
        <template slot-scope="{row}">
          {{ row.beginTime ? utils.resolvingDate(row.beginTime) : '-'}}
        </template>
      </el-table-column>

      <el-table-column label="结束时间" align="left" prop="endTime">
        <template slot-scope="{row}">
          {{ row.endTime ? utils.resolvingDate(row.endTime) : '-'}}
        </template>
      </el-table-column> -->

      <el-table-column label="操作" align="left" width="125px">
        <template slot-scope="{ row }">
          <!-- <el-button
            type="text"
            @click="viewDetail(row)"
            >详情
          </el-button> -->
          <el-button
            type="text"
            @click="edit(row)"
            v-hasPermi="['system:time:edit']"
            >编辑
          </el-button>
          <!-- <el-button
            v-if="row.enable == 0"
            type="text"
            @click="sync(row)"
            v-hasPermi="['system:time:sync']"
            >同步
          </el-button> -->

          <el-button
            type="text"
            @click="handleShake(row)"
            hasPermi
            :disabled="row.isDisabledForShake"
            v-hasPermi="['system:time:editStatus']"
          >
            <span v-if="row.enable == 1">启用</span>
            <span v-if="row.enable == 0">停用</span>
          </el-button>
          <el-button
            type="text"
            @click="del([row.timeSynId])"
            v-hasPermi="['system:time:delete']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />

    <editDialog ref="editDialog" @refresh="getList" />
    <time-detail ref="timeDetail"></time-detail>
  </common-list>
</template>

<script>
import editDialog from "./components/editDialog";
import TimeDetail from "./components/timeDetail";
import {
  time_config_list,
  time_config_remove,
  time_config_remove_check,
  time_config_sync,
  time_config_edit,
  time_config_editStatus,
} from "@/api/management/configure/index.js";
import BaseList from "@/mixins/BaseList";

export default {
  mixins: [BaseList],
  components: { editDialog, TimeDetail },
  props: {},
  data() {
    return {
      showSearch: true, // 查询部分显示
      total: 1, // 总条数
      loading: false, // table 加载中
      list: [], // 数据列
      selectionIds: [], // 列表选中数据id
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      query: {
        pageNum: 1,
        pageSize: 10,
        enable: null,
      }, // 查询条件
    };
  },
  watch: {},
  computed: {},
  methods: {
    /**
     * 选择
     */
    handleSelectionChange(selection) {
      this.selectionIds = selection.map((item) => item.timeSynId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /**
     * 获取列表
     */
    getList() {
      this.loading = true;
      time_config_list({ ...this.query })
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows.reduce((r, c) => {
              r.push({
                ...c,
                isDisabledForShake: false,
              });
              return r;
            }, []);
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * 查询
     */
    handleQuery() {
      this.query.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询
     */
    resetQuery() {
      this.query = this.$options.data().query;
    },

    viewDetail(row) {
      this.$refs.timeDetail.show(row);
    },
    /**
     * 添加/编辑
     */
    edit(row) {
      if (row) {
        let enable = row.enable;
        if (enable == 0) {
          this.$GlobalErrorConfirm("编辑失败", "启用状态下不允许编辑");
          return;
        }
      }
      this.$refs.editDialog.show(row ? { ...row } : {});
    },
    changeStatus(row) {
      return new Promise((resolve, reject) => {
        let enable = row.enable == 0 ? "1" : "0";
        time_config_editStatus({
          timeSynId: row.timeSynId,
          enable: enable,
        })
          .then((res) => {
            if (res && res.code === 200) {
              this.$GlobalSuccessMessage("操作成功");
              this.query.pageNum = 1;
              this.getList();
              resolve(res);
            } else {
              reject();
              this.$GlobalErrorConfirm("操作失败", res.msg);
            }
          })
          .catch(() => reject());
      });
    },

    /**
     *
     * 删除
     */
    del(ids) {
      time_config_remove_check({ ids: ids.join(",") }).then((res) => {
        if (res && res.code === 500 && res.data === 0) {
          this.$GlobalErrorConfirm("删除失败", res.msg);
        } else {
          this.$GlobalDeleteConfirm()
            .then(() => {
              time_config_remove({ ids: ids.join(",") }).then((res) => {
                if (res && res.code === 200) {
                  console.log(111);
                  this.$GlobalSuccessMessage("删除成功");
                  if (
                    this.query.pageNum > 1 &&
                    ids.split(",").length === this.list.length
                  ) {
                    this.query.pageNum -= 1;
                  }
                  this.getList();
                } else {
                  this.$GlobalErrorConfirm("删除失败", res.msg);
                }
              });
            })
            .catch(() => {});
        }
      });
    },

    /**
     * 同步
     */
    sync(row) {
      time_config_sync(row).then((res) => {
        if (res && res.code === 200) {
          this.getList();
          this.$GlobalSuccessMessage("同步成功");
        } else {
          this.$GlobalErrorConfirm("同步失败", res.msg);
        }
      });
    },
  },
  created() {
    this.getList();
  },
  mounted() {},
};
</script>
<style lang="scss"></style>
